package com.visikard.ui.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.item.ItemTransactionHistory;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;
import com.visikard.widget.expandedlistview.BaseExpandedAdapter;
import com.visikard.widget.expandedlistview.ExpandingLayout;

public class TransactionHistoryCouponAdapter extends BaseExpandedAdapter<ItemTransactionHistory> {

	Context context;
	List<ItemTransactionHistory> data;
	LayoutInflater inflater;
	DateFormat formatter;
	DateFormat dateOnlyFormatter;
	DisplayMetrics metrics;
	DateFormat timeFormat;
	Handler mHandler;

	public TransactionHistoryCouponAdapter(Context ct, List<ItemTransactionHistory> dt, Handler mHandler) {
		super(ct, R.layout.item_transaction_history_coupon_kard, dt);
		context = ct;
		data = dt;
		this.context = ct;
		this.data = dt;
		this.mHandler = mHandler;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		formatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR, Locale.getDefault());
		dateOnlyFormatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR_MONTH_DATE, Locale.getDefault());
		timeFormat = new SimpleDateFormat(Constant.TIME_FULL_FORMAT_STR, Locale.getDefault());
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public ItemTransactionHistory getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {
		TextView mUsername, mGkValue, mTextHeader, mTimeZone, mAddress, mRedeemCode, mRedeemDate;
		ImageView imgKard, imgGiftKard, imgArrow;
		RelativeLayout lyHeader;
	}

	@Override
	public View getViewItem(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_transaction_history_coupon_kard, parent, false);
//			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();		
			
			viewHolder.lyHeader = (RelativeLayout) convertView.findViewById(R.id.item_transaction_header);			
			viewHolder.mTextHeader = (TextView) convertView.findViewById(R.id.transaction_text_header);
			viewHolder.mTimeZone = (TextView) convertView.findViewById(R.id.text_transaction_timezone);
			viewHolder.mUsername = (TextView) convertView.findViewById(R.id.text_transaction_username);
			viewHolder.mGkValue = (TextView) convertView.findViewById(R.id.txt_percentage_off);
			viewHolder.mAddress = (TextView) convertView.findViewById(R.id.txt_redeem_address);
			viewHolder.mRedeemCode = (TextView) convertView.findViewById(R.id.txt_redemption_code);
			viewHolder.mRedeemDate = (TextView) convertView.findViewById(R.id.txt_redemption_date);			
			

			viewHolder.imgKard = (ImageView) convertView
					.findViewById(R.id.image_kard);
			viewHolder.imgGiftKard = (ImageView) convertView
					.findViewById(R.id.image_kard_gift_kard);
			viewHolder.imgArrow = (ImageView) convertView
					.findViewById(R.id.icon_more);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ItemTransactionHistory item = (ItemTransactionHistory) getItem(position);
		
		if (item != null) {

			viewHolder.mUsername.setText(item.getUsername());
			
			if (!item.getKardImage().isEmpty())
				UILImageUtils.loadImage(viewHolder.imgKard, //image view
						item.getKardImage(), //url
						1, //size of view
						BitmapUtils.ROUND_ALL, //round all 
						4, // round size
						3, //shadow
						R.drawable.kard_shadow_default_horizontal, //shadow resource 
						0, 0);

			if (!item.getImageGiftKard().isEmpty())
				UILImageUtils.loadImage(viewHolder.imgGiftKard, //image view
						item.getImageGiftKard(), //url
						1, //size of view
						BitmapUtils.ROUND_ALL, //round all 
						4, // round size
						3, //shadow
						R.drawable.kard_shadow_default_horizontal, //shadow resource 
						0, 0);
			
			viewHolder.mGkValue.setText(item.getOriginalAmount());

			try {
//				Date today = dateOnlyFormatter.parse(dateOnlyFormatter
//						.format(new Date()));
//				Date date = dateOnlyFormatter
//						.parse(dateOnlyFormatter.format(formatter.parse(item
//								.getDateTimeWithTimezone())));
				Date today = new Date();
				Date date = new Date(item.getDateTime());
				
				if (position < 1) {
					if (Utils.compareDateOnly(date, today) == 0) {
						String strDate = context.getResources().getString(R.string.literal_today); 
						viewHolder.mTextHeader.setText(strDate);
					}else{
						Calendar cal = Calendar.getInstance();
						cal.setTime(date);
						viewHolder.mTextHeader.setText(dateOnlyFormatter.format(date));
					}
					viewHolder.lyHeader.setVisibility(View.VISIBLE);
				} else {
//					Date date_above = dateOnlyFormatter
//					.parse(dateOnlyFormatter.format(formatter
//							.parse(data.get(position - 1)
//									.getDateTimeWithTimezone())));
					Date date_above = new Date(data.get(position - 1).getDateTime());
					if (Utils.compareDateOnly(date, date_above) != 0) {
						Calendar cal = Calendar.getInstance();
						cal.setTime(date);
						viewHolder.mTextHeader.setText(dateOnlyFormatter.format(date));
						viewHolder.lyHeader.setVisibility(View.VISIBLE);
					} else
						viewHolder.lyHeader.setVisibility(View.GONE);
				}
				
				viewHolder.mTimeZone.setText(timeFormat.format(formatter.parse(item.getDateTimeWithTimezone())));
				viewHolder.mRedeemDate.setText(dateOnlyFormatter.format(formatter.parse(item.getDateTimeWithTimezone())));

			} catch (Exception e) {
				e.printStackTrace();
			}			
			
			viewHolder.mAddress.setText(item.getAddress());
			viewHolder.mRedeemCode.setText(item.getRedeemptionCode());			
			
//			if(item.isExpanded()){
//				viewHolder.imgArrow.setImageResource(R.drawable.arrow_bottom);
//			}else{
//				viewHolder.imgArrow.setImageResource(R.drawable.arow_detail_page);
//			}
		}
		return convertView;
	}

	@Override
	public ExpandingLayout getExpandingLayout(View view) {
		View result = view.findViewById(R.id.item_transaction);
		if(result != null && result instanceof ExpandingLayout){
			return (ExpandingLayout)result;
		}
		return null;
	}

}
